package com.supersit.greensproject.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.supersit.greensproject.AsyncTask.UpdateBaseListAndMaketList;
import com.supersit.greensproject.DB.XUtilsDbManager;
import com.supersit.greensproject.R;
import com.supersit.greensproject.adapter.SubjectDataAdapter;
import com.supersit.greensproject.application.MyApplication;
import com.supersit.greensproject.entity.BaseBase;
import com.supersit.greensproject.fragment.EnteringDataFragment;
import com.supersit.greensproject.fragment.SelectedDataFragment;
import com.supersit.greensproject.resources.Authority;
import com.supersit.greensproject.resources.Resources;
import com.supersit.greensproject.utils.NetUtils;
import com.supersit.greensproject.view.MyTitleBar;
import com.supersit.greensproject.view.ShowDialog;
import com.zhy.autolayout.AutoLayoutActivity;

import org.xutils.DbManager;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 主体数据
 * Created by cong on 2016/7/13.
 */
@ContentView(R.layout.activity_subject_data)
public class SubjectDataActivity extends AutoLayoutActivity {
    private MyTitleBar titleBar;
    private Context context;
    @ViewInject(R.id.lv_subject_list)
    private ListView lvSubjectData;
    private ArrayList<Map<Integer, String>> itemData;
    private SubjectDataAdapter adapter;
    private ArrayList<BaseBase> vbList;
    private ShowDialog showDialog;
    private DbManager dbManager;
    private int[] baseAuthority;
    private int[] maketAuthority;
    private UpdateBaseListAndMaketList updateBaseListAndMaketList;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        context = this;
        showDialog = new ShowDialog(context);
        dbManager = XUtilsDbManager.getInstance();
        baseAuthority = Authority.getBaseAuthority(context);
        maketAuthority = Authority.getMaketAuthority(context);
        updateBaseListAndMaketList = new UpdateBaseListAndMaketList(baseAuthority, maketAuthority, context);
        setTitleBar();
        bindListener();
        setListViewData();
        if (NetUtils.isConnected(context))
            updateBaseListAndMaketList.updateBaseData();//更新数据
    }

    private void setTitleBar() {
        titleBar = new MyTitleBar(context, findViewById(R.id.subject_data_title));
        titleBar.setTitleText(getResources().getString(R.string.subject_title));
        titleBar.setChildVisibility(false, titleBar.getmImgRight());

    }

    private void bindListener() {
        titleBar.setImg1Listener(onClickListener);
        lvSubjectData.setOnItemClickListener(onItemClickListener);
    }

    private void setListViewData() {
        itemData = new ArrayList<Map<Integer, String>>();
        for (int i = 0; i < baseAuthority.length; i++) {
            for (int j = 0; j < Resources.subjectMenu.length; j++) {
                if (baseAuthority[i] == j) {
                    Map<Integer, String> map = new HashMap<Integer, String>();
                    map.put(baseAuthority[i], Resources.subjectMenu[baseAuthority[i]]);
                    itemData.add(map);
                }
            }
        }
        int[] maketAuthority = Authority.getMaketAuthority(context);
        if (maketAuthority.length > 0) {
            Map<Integer, String> map = new HashMap<Integer, String>();
            map.put(5, Resources.subjectMenu[5]);
            itemData.add(map);
        }
        adapter = new SubjectDataAdapter(context
                , itemData, Resources.NoMessige);
        lvSubjectData.setAdapter(adapter);

    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.title_bar_left:
                    finish();
                    break;
            }
        }
    };
    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int num = 0;
            Map<Integer, String> item = itemData.get(position);
            for (int key : item.keySet()) {
                num = key;
            }
            SelectedDataFragment selectedDataFragment = new SelectedDataFragment();
            EnteringDataFragment enteringDataFragment = new EnteringDataFragment();
            Intent intent = new Intent(context, FragmentActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("baseType", num);
            switch (num) {
                case 0:
                    MyApplication.list.clear();
                    MyApplication.list.add(selectedDataFragment);
                    MyApplication.list.add(enteringDataFragment);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case 1:
                    MyApplication.list.clear();
                    MyApplication.list.add(selectedDataFragment);
                    MyApplication.list.add(enteringDataFragment);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;

                case 2:
                    MyApplication.list.clear();
                    MyApplication.list.add(selectedDataFragment);
                    MyApplication.list.add(enteringDataFragment);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case 3:
                    MyApplication.list.clear();
                    MyApplication.list.add(selectedDataFragment);
                    MyApplication.list.add(enteringDataFragment);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case 4:
                    MyApplication.list.clear();
                    MyApplication.list.add(selectedDataFragment);
                    MyApplication.list.add(enteringDataFragment);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case 5:
                    startActivity(new Intent(context, QueryMaketSubjectDataActivity.class));
                    break;
                default:
                    break;


            }
        }
    };
}
